import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:window_manager/window_manager.dart';

class HeaderPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HeaderPageState();
  }
}

class TimeProvider extends ChangeNotifier {
  String _currentTimeStr = '';
  String _currentDayStr = '';

  String get currentTimerStr => _currentTimeStr;

  String get currentDayStr => _currentDayStr;

  TimeProvider() {
    Timer.periodic(const Duration(seconds: 1), (_) {
      var dateTime = DateTime.now();
      var hour = dateTime.hour.toString();
      var minute = dateTime.minute.toString();
      var sec = dateTime.second.toString();
      var year = dateTime.year.toString();
      var month = dateTime.month.toString();
      var day = dateTime.day.toString();
      _currentTimeStr =
          '${handleTimeFormat(hour)}:${handleTimeFormat(minute)}:${handleTimeFormat(sec)}';
      _currentDayStr =
          '${handleTimeFormat(year)}-${handleTimeFormat(month)}-${handleTimeFormat(day)}';
      notifyListeners();
    });
  }

  String handleTimeFormat(String input) {
    return input.length == 1 ? '0$input' : input;
  }
}

class _HeaderPageState extends State<HeaderPage> {
  final _default_control_color = Colors.white;
  final _hover_color = Colors.pinkAccent;
  var _close_hover = false;
  var _minize_hover = false;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 200,
      child: Column(
        children: [
          Container(
            color: Colors.red.withOpacity(0.01),
            height: 30,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                MouseRegion(
                  onEnter: (_) {
                    setState(() {
                      _minize_hover = true;
                    });
                  },
                  onHover: (_) {
                    setState(() {
                      _minize_hover = false;
                    });
                  },
                  child: IconButton(
                      onPressed: () {
                        windowManager.minimize();
                      },
                      icon: Icon(
                        Icons.minimize,
                        color: _minize_hover
                            ? _hover_color
                            : _default_control_color,
                      )),
                ),
                MouseRegion(
                  onEnter: (_) {
                    setState(() {
                      _close_hover = true;
                    });
                  },
                  onExit: (_) {
                    setState(() {
                      _close_hover = false;
                    });
                  },
                  child: IconButton(
                    onPressed: () {
                      windowManager.close();
                    },
                    icon: const Icon(Icons.close),
                    color: _close_hover ? _hover_color : _default_control_color,
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            flex: 3,
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    context.watch<TimeProvider>().currentTimerStr,
                    style: TextStyle(
                        fontSize: 50,
                        fontWeight: FontWeight.w500,
                        foreground: Paint()..color = Colors.white),
                  ),
                  Text(
                    context.watch<TimeProvider>().currentDayStr,
                    style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                        foreground: Paint()..color = Colors.white),
                  )
                ],
              ),
            ),
          ),
          Expanded(
            flex: 2,
            child: Row(
              children: [
                Expanded(flex: 1, child: Container()),
                Expanded(
                    flex: 2,
                    child: Container(
                      color: Colors.white.withOpacity(0.6),
                      child: TextField(
                        onSubmitted: (s) {
                          launchUrl(
                              Uri.parse('https://www.bing.com/search?q=$s'));
                        },
                        textAlignVertical: TextAlignVertical.center,
                        autofocus: false,
                        cursorColor: Colors.pinkAccent,
                        decoration: InputDecoration(
                          iconColor: Colors.pinkAccent,
                          prefixIcon: const Icon(Icons.search),
                          border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(10)),
                        ),
                      ),
                    )),
                Expanded(flex: 1, child: Container()),
              ],
            ),
          ),
        ],
      ),
    );
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(DiagnosticsProperty<bool>('_close_hover', _close_hover));
  }
}
